package com.yylending.las.authority.gestures;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.common.widgets.CommonDialog;

public class GesturesActivity extends BaseActivity implements GesturesPresenter.View, CompoundButton.OnCheckedChangeListener, View.OnClickListener {
	private final static String TAG = "GesturesActivity";

	@BindView(R.id.gestures_chb)
	CheckBox mToggle;
	@BindView(R.id.gestures_modify)
	TextView mCreateOrModify;
	@BindView(R.id.gestures_reset)
	TextView mGesturesReset;
	@BindView(R.id.gestures_modify_split)
	View mGesturesModifySplit;
	@BindView(R.id.gestures_switch_split)
	LinearLayout mGesturesSwitchSplit;

	public static void start(Activity aty) {
		Intent i = new Intent(aty, GesturesActivity.class);
		aty.startActivity(i);
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gestures);
		ButterKnife.bind(this);
		setTitle(R.string.title_activity_gestures);
		//mToggle.setOnCheckedChangeListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		refreshStatus();
		readStatus();
		changeStatus(mToggle.isChecked());
	}

	private void refreshStatus() {
		if (YJKD.getInstance().hadLockPattern()) {
			mCreateOrModify.setText(R.string.gesture_modify);
		} else {
			mCreateOrModify.setText(R.string.gesture_create);
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
		CLog.i(TAG, "手势是否打开-onCheckedChanged->" + checked);
		judgeCheckbox(checked);
	}

	private void judgeCheckbox(boolean checked){
		if (checked) {
			if(YJKD.getInstance().isFingerprintOpen() && YJKD.getInstance().hasFingerprint()){
				showTwoBtnDialog(getString(R.string.close_fingerprint_hint), getString(R.string.confirm), getString(R.string.cancel), mCloseFingerprintDialog, true);
			}else{
				ModifyGesturesActivity.start(this, ModifyGesturesActivity.GESTURE_SET);
				changeStatus(true);
			}
		} else {
			ModifyGesturesActivity.start(GesturesActivity.this, ModifyGesturesActivity.GESTURE_CLOSE);
			changeStatus(false);
		}
	}

	private void changeStatus(boolean isChecked) {
		if (isChecked) {
			mCreateOrModify.setVisibility(View.VISIBLE);
			mGesturesReset.setVisibility(View.VISIBLE);
			mGesturesSwitchSplit.setVisibility(View.VISIBLE);
			mGesturesModifySplit.setVisibility(View.VISIBLE);
		} else {
			mCreateOrModify.setVisibility(View.GONE);
			mGesturesReset.setVisibility(View.GONE);
			mGesturesSwitchSplit.setVisibility(View.GONE);
			mGesturesModifySplit.setVisibility(View.GONE);
		}
	}

	private void readStatus() {
		boolean status = GesturesPresenter.readStatus();
		CLog.i(TAG, "手势是否打开-->" + status);
		mToggle.setChecked(status);
	}

	@OnClick({R.id.gestures_modify, R.id.gestures_reset, R.id.gestures_chb})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.gestures_modify:
				if (YJKD.getInstance().hadLockPattern()) {
					CLog.i(TAG, "已有手势密码");
					ModifyGesturesActivity.start(this, ModifyGesturesActivity.GESTURE_MODIFY);
				} else {
					CLog.i(TAG, "没有手势密码");
					ModifyGesturesActivity.start(this, ModifyGesturesActivity.GESTURE_SET);
				}
				break;
			case R.id.gestures_reset:
				ResetGesturesActivity.start(this);
				break;
			case R.id.gestures_chb:
				judgeCheckbox(mToggle.isChecked());
				break;
		}
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	private CommonDialog.ButtonClickListener mCloseFingerprintDialog = new CommonDialog.ButtonClickListener() {
		@Override
		public void onCancel(CommonDialog dialog) {
			dialog.dismiss();
		}

		@Override
		public void onConfirm(CommonDialog dialog) {
			dialog.dismiss();
			YJKD.getInstance().setFingerprintOpen(false);
			ModifyGesturesActivity.start(GesturesActivity.this, ModifyGesturesActivity.GESTURE_SET);
		}
	};
}